% fname: routing_xy__topology_8x8__.m
% ./noxim -routing xy -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -buffer 2 -hs 27 0.2 -hs 28 0.2 -hs 35 0.2 -hs 36 0.2 

function [max_pir, max_throughput, min_delay] = routing_xy__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
           0.0018         17.144      0.0144516             98            896           7168
           0.0018        17.6419       0.015377            100            969           7750
           0.0018        17.1454      0.0157021             92            942           7537
           0.0018        16.8143      0.0154095             93            894           7150
           0.0018        16.3685      0.0146653             75            909           7274
           0.0018        17.7609      0.0151695            104            895           7160
           0.0018        16.9217       0.015256             98            945           7567
           0.0018        17.1633      0.0155271             84            931           7453
           0.0018        16.8048      0.0145271            108            871           6973
           0.0018        17.6318      0.0154254            145            956           7651
           0.0018        16.8729      0.0156063             85            936           7491
           0.0018        16.7312      0.0165943            128            945           7567
           0.0018          16.11       0.014375             96            891           7130
           0.0018         15.385      0.0141349             76            891           7124
           0.0018        16.6057      0.0148246             82            918           7353
           0.0018        17.5086      0.0152889            115            932           7461
           0.0018        17.0772      0.0157667             73            945           7568
           0.0018        18.2583       0.015125            188            937           7502
           0.0018        17.5211      0.0156694            131            971           7772
           0.0018        17.3781      0.0156149            126            968           7745
            0.002         17.995      0.0155332            137            994           7953
            0.002        17.5418      0.0171895            100           1065           8526
            0.002        17.3919      0.0153906            100            985           7880
            0.002        18.4577      0.0163972            153           1016           8133
            0.002         18.139      0.0165887            116           1029           8228
            0.002        19.2265       0.016498            103           1020           8183
            0.002        18.4883      0.0166309            103           1065           8515
            0.002        17.1395      0.0162722            128           1011           8071
            0.002        18.4581      0.0167661            181           1039           8316
            0.002        17.0447      0.0157188            146           1006           8048
            0.002        17.6802      0.0157285            117           1007           8053
            0.002        19.6294      0.0163065            141           1012           8088
            0.002        18.0099      0.0160397             83           1010           8084
            0.002        17.9202       0.016131            204           1015           8130
            0.002        20.2514      0.0171012            152           1078           8619
            0.002        18.3645      0.0181048            108           1122           8980
            0.002        20.2392      0.0158965            206           1016           8139
            0.002        19.6385       0.016994            198           1054           8429
            0.002        16.9904      0.0165079             85           1041           8320
            0.002         18.324      0.0167681            135           1040           8317
           0.0022        18.9772      0.0172828            157           1053           8434
           0.0022        21.1157      0.0184274            216           1141           9140
           0.0022        21.6361      0.0188165            145           1168           9333
           0.0022        20.5609      0.0180879            133           1157           9261
           0.0022        17.9443      0.0174121             99           1113           8915
           0.0022        21.7513       0.017871            188           1126           9007
           0.0022        21.4411      0.0178185            293           1104           8838
           0.0022        19.3333      0.0175234            126           1122           8972
           0.0022        21.5059      0.0192258            211           1192           9536
           0.0022        18.3039      0.0183893            138           1122           8974
           0.0022        19.2381      0.0174766            137           1117           8948
           0.0022        21.9515      0.0183044            286           1135           9079
           0.0022         19.691      0.0169683            167           1068           8552
           0.0022        20.0891      0.0177285            159           1134           9077
           0.0022        18.7878      0.0167258            103           1037           8296
           0.0022        21.3705      0.0172063            155           1085           8672
           0.0022        21.0604       0.018131            162           1125           8993
           0.0022        18.5326       0.018082            138           1104           8824
           0.0022         18.036      0.0183488            125           1138           9101
           0.0022        18.4601      0.0174012            137           1078           8631
           0.0024         19.626       0.019506            141           1230           9831
           0.0024        23.2524      0.0197282            206           1244           9943
           0.0024        23.5418      0.0196774            251           1220           9760
           0.0024        20.0449      0.0200881            109           1225           9803
           0.0024        22.0769       0.018994            288           1197           9573
           0.0024        21.2688      0.0195098            120           1250           9989
           0.0024        20.2875      0.0185469            102           1186           9496
           0.0024        21.3166      0.0199551            141           1276          10217
           0.0024        25.5192      0.0202956            162           1279          10229
           0.0024        21.7927      0.0194643            162           1225           9810
           0.0024        21.8923      0.0192988            142           1235           9881
           0.0024        20.3135      0.0194881            147           1228           9822
           0.0024        22.0057      0.0192578            111           1234           9860
           0.0024        26.5968      0.0199008            216           1255          10030
           0.0024        22.9633      0.0204336            212           1307          10462
           0.0024         21.866      0.0199221            133           1216           9722
           0.0024        23.9466       0.019293            248           1236           9878
           0.0024        23.0482       0.019754            194           1223           9798
           0.0024         22.038      0.0199395            139           1238           9890
           0.0024        22.6807      0.0191754            204           1190           9511
           0.0026        24.0791       0.020875            197           1315          10521
           0.0026        24.3912      0.0209805            188           1342          10742
           0.0026        25.3237      0.0208552            184           1313          10511
           0.0026        31.5614      0.0212539            265           1361          10882
           0.0026        22.6654      0.0208214            200           1312          10494
           0.0026        21.4183       0.020668            176           1260          10086
           0.0026        24.0324      0.0205893            158           1297          10377
           0.0026        24.3656      0.0209941            195           1343          10749
           0.0026        28.6599       0.022972            255           1332          10659
           0.0026        25.6789      0.0210524            350           1305          10442
           0.0026        26.5506      0.0214742            395           1353          10823
           0.0026        25.0681       0.020627            204           1321          10561
           0.0026        23.9446      0.0205723            230           1317          10533
           0.0026        26.7284       0.021244            297           1340          10707
           0.0026        26.1368      0.0208027            174           1330          10651
           0.0026        26.1078      0.0217246            240           1391          11123
           0.0026          23.73       0.021875            200           1378          11025
           0.0026        27.0194      0.0209316            251           1338          10717
           0.0026        21.9863      0.0202787            196           1237           9896
           0.0026        24.6557      0.0212637            234           1362          10887
           0.0028        28.2879      0.0232996            225           1466          11743
           0.0028        26.0487      0.0218552            227           1377          11015
           0.0028        29.7381      0.0223164            266           1428          11426
           0.0028        29.1527      0.0218984            334           1401          11212
           0.0028        32.2504      0.0222637            247           1426          11399
           0.0028        29.0112      0.0230423            260           1429          11429
           0.0028        32.3742      0.0230977            623           1478          11826
           0.0028        26.6314      0.0221582            195           1419          11345
           0.0028        25.4978      0.0215762            265           1380          11047
           0.0028        38.2488      0.0233379            795           1495          11949
           0.0028        25.3704      0.0227656            213           1458          11656
           0.0028        35.9987      0.0245282            317           1521          12166
           0.0028        31.2133      0.0221465            255           1416          11339
           0.0028        34.4022      0.0232103            352           1462          11698
           0.0028        25.9395      0.0224746            247           1439          11507
           0.0028        27.5319      0.0223047            216           1427          11420
           0.0028        29.1444      0.0227285            242           1454          11637
           0.0028        29.4106      0.0227227            296           1454          11634
           0.0028        45.7445      0.0235293            558           1507          12047
           0.0028        29.2657      0.0229004            269           1464          11725
            0.003        39.8938      0.0238516            503           1526          12212
            0.003         40.657      0.0251687            389           1586          12685
            0.003        35.5102      0.0237832            261           1521          12177
            0.003        40.3904      0.0251349            598           1583          12668
            0.003        30.6004      0.0236429            280           1489          11916
            0.003        38.1845      0.0235176            655           1507          12041
            0.003        32.4291      0.0234922            423           1503          12028
            0.003        39.8843      0.0248477            337           1591          12722
            0.003        34.5056      0.0238359            370           1523          12204
            0.003        58.7729      0.0240215           1027           1537          12299
            0.003          34.01      0.0234023            424           1498          11982
            0.003        32.8989      0.0236504            299           1514          12109
            0.003        38.2233      0.0235039            351           1505          12034
            0.003        34.4861      0.0249153            268           1545          12358
            0.003        31.6696      0.0231641            320           1483          11860
            0.003        31.7851       0.023498            263           1503          12031
            0.003        32.8708      0.0240645            267           1540          12321
            0.003        41.9303      0.0246563            348           1578          12624
            0.003        52.4954       0.025416            728           1627          13013
            0.003        36.8013       0.024996            362           1575          12598
           0.0032        33.3126      0.0249023            378           1593          12750
           0.0032        46.1377      0.0261094            427           1670          13368
           0.0032        60.1438      0.0257168            796           1648          13167
           0.0032        44.1115      0.0251191            711           1606          12861
           0.0032         48.042      0.0256562            421           1641          13136
           0.0032        42.3364      0.0251309            677           1608          12867
           0.0032        41.6862      0.0255488            408           1635          13081
           0.0032        55.3916      0.0258184           1123           1652          13219
           0.0032        44.1863      0.0252363            579           1616          12921
           0.0032        54.7594      0.0257852            805           1650          13202
           0.0032        40.5628      0.0270437            332           1704          13630
           0.0032        53.6745       0.025252            559           1616          12929
           0.0032        56.9683      0.0256777            995           1642          13147
           0.0032        46.5276      0.0258086            490           1651          13214
           0.0032        50.6336      0.0262056            565           1624          12998
           0.0032        51.4823      0.0247363            701           1584          12665
           0.0032        42.8367      0.0269698            395           1672          13377
           0.0032        55.5447      0.0260176            793           1665          13321
           0.0032        47.8013      0.0259414            414           1661          13282
           0.0032        50.2039      0.0254375            987           1628          13024
           0.0034        46.5374      0.0268945            510           1723          13770
           0.0034        134.329      0.0271699           2609           1739          13911
           0.0034        54.4864      0.0274805            803           1760          14070
           0.0034        170.983      0.0283105           4373           1813          14495
           0.0034        68.0869      0.0271152           1506           1737          13883
           0.0034        62.7641      0.0277758            814           1751          13999
           0.0034        100.303      0.0276984           2295           1745          13960
           0.0034        69.1949      0.0267324            904           1709          13687
           0.0034        67.3826      0.0279531            832           1788          14312
           0.0034        48.7255      0.0268552            868           1694          13535
           0.0034         33.252      0.0251523            405           1611          12878
           0.0034        105.854      0.0277012           2421           1773          14183
           0.0034        78.2243       0.027168           1798           1739          13910
           0.0034        149.215       0.027123           3624           1736          13887
           0.0034         66.004      0.0275566           1150           1763          14109
           0.0034        56.0595      0.0272421            991           1715          13730
           0.0034        56.8239      0.0273418            612           1749          13999
           0.0034        108.412      0.0276953           2505           1773          14180
           0.0034          178.6      0.0288027           4309           1843          14747
           0.0034        50.6427      0.0273438            666           1749          14000
           0.0036        171.072      0.0291151           4594           1834          14674
           0.0036        64.8161      0.0274405            909           1729          13830
           0.0036         97.272      0.0281758           1097           1805          14426
           0.0036        158.323      0.0289961           2097           1854          14846
           0.0036        96.9586      0.0286523           1521           1834          14670
           0.0036          101.3      0.0288184           2359           1844          14755
           0.0036         151.13      0.0286445           2001           1834          14666
           0.0036        83.9311      0.0278848           1494           1785          14277
           0.0036        71.9102      0.0274844           1859           1760          14072
           0.0036        124.167      0.0286452           1975           1776          14208
           0.0036        56.5453      0.0278105            547           1779          14239
           0.0036        176.722      0.0304881           4833           1920          15366
           0.0036        144.274      0.0295801           4216           1894          15145
           0.0036          159.3      0.0293965           4477           1882          15051
           0.0036        68.3837      0.0280313            994           1793          14352
           0.0036        135.019      0.0289785           3705           1854          14837
           0.0036        60.1194       0.027457            830           1759          14058
           0.0036        90.8083      0.0294023           1550           1883          15054
           0.0036        135.006      0.0290684           3867           1860          14883
           0.0036        76.9817      0.0282109           1377           1805          14444
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
